<template>
	<div>

		<Search :lookup="data.lookup" @investigate="inves" />

		<div>
			<el-button type="primary" @click="exportExcel"> {{ $t('public.export') }} Excel</el-button>
		</div>

		<div class="mt-2" v-loading="data.mtloading">

			<el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
				:cell-style="{ 'text-align': 'center' }" class="w-full">

				<el-table-column prop="machine_id" :label="$t('machine.code')" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.machine_id || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="machine_name" :label="$t('machine.name')" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.machine_name || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="check_stock" :label="$t('report.Inventory.quantity')" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.check_stock || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="stock_reserve" :label="$t('report.Inventory.backup')" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.stock_reserve || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="creator_nickname" :label="$t('report.Inventory.stocktaking')" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.creator_nickname || '--' }}
					</template>
				</el-table-column>

				<el-table-column prop="create_time" :label="$t('date.inventoryTime')" show-overflow-tooltip>
					<template #default="scope">
						{{ getPlevel.gettime(scope.row.create_time, 3) }}
					</template>
				</el-table-column>
			</el-table>

			<PageNum :total="data.total" :page="data.count" @exposeData="mcurrent" @fraction="mcfraction" />

		</div>
		<exports :dialogVisible="dialogExport" @close="dialogExport = false" />
	</div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import getPlevel from "@/utils/level";
import { getstockcountList, exportCheckStockCount } from '@/api/report';
import { ElMessage, ElMessageBox } from "element-plus";
import exports from '@/components/export.vue';
const dialogExport = ref(false)
let data = reactive({
	mtloading: false, // 加载
	lookup: [ // 筛选栏
		{ title: $t('machine.code'), type: 'mId', value: '', key: 'm_id' },
		{ title: $t('report.Inventory.stocktaks'), type: 'input', value: '', key: 'creator_nickname' },
		{ title: $t('date.inventoryTime'), type: "times", value: "", key: "time" }
	],
	list: [], // 列表信息
	count: {
		m_id: '',
		creator_nickname: '',
		create_time: '',
		page: 1,
		pageNum: 15
	},
	total: 0, // 总条数
})
let exportExcel = () => {
	let obj = {
		m_id: data.count.m_id,
		creator_nickname: data.count.creator_nickname,
		create_time: data.count.create_time
	}
	ElMessageBox.confirm($t('message.isExport')).then(res => {
		exportCheckStockCount(obj).then(res => {
			if (res.state == 200) {
				dialogExport.value = true
				return
			}
			ElMessage({
				message: $t('message.networkFailed'),
				type: 'error'
			})
		}).catch(err => {
			console.log(err)
		})
	}).catch(err => {
		console.log('取消')
	})
}
// 获取列表信息
let getlist = () => {
	data.mtloading = true
	getstockcountList(data.count).then(res => {
		data.mtloading = false
		if (res.state == 200) {
			data.list = res.data.data
			data.total = res.data.total
		}
	})
}

// 查询回调
let inves = (e) => {
	data.count.create_time = ''
	for (let key in e) {
		if (key != 'time') {
			data.count[key] = e[key]
		}
	}
	if (e.time != '') {
		data.count.create_time = e.time[0]  + '~' + e.time[1]
	}
	data.count.page = 1
	data.count.pageNum = 15
	getlist()
}

// 弹窗页数回调
let mcurrent = (e) => {
	data.count.page = e
	getlist()
}

// 弹窗条数回调
let mcfraction = (e) => {
	data.count.pageNum = e
	getlist()
}

getlist()

</script>
